home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / bash-1.12 / sun4.md / builtins / fc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-12-15  |  14.2 KB  |  648 lines

  1. /* fc.c, created from fc.def. */
  2. #line 23 "builtins/fc.def"
  3.  
  4. #line 47 "builtins/fc.def"
  5.  
  6. #include <stdio.h>
  7. #include <sys/param.h>
  8. #include <sys/types.h>
  9. #include <sys/stat.h>
  10. #include <sys/file.h>
  11. #include <errno.h>
  12. #include "../shell.h"
  13. #include "../builtins.h"
  14. #include "../flags.h"
  15. #include "../maxpath.h"
  16. #include <readline/history.h>
  17.  
  18. #if defined (NULL)
  19. #undef NULL
  20. #endif
  21. #define NULL 0
  22.  
  23. extern int errno;
  24.  
  25. /* **************************************************************** */
  26. /*                                    */
  27. /*    The K*rn shell style fc command (Fix Command)            */
  28. /*                                    */
  29. /* **************************************************************** */
  30.  
  31. /* fc builtin command (fix command) for Bash for those who
  32.    like K*rn-style history better than csh-style.
  33.  
  34.      fc [-e ename] [-nlr] [first] [last]
  35.  
  36.    FIRST and LAST can be numbers specifying the range, or FIRST can be
  37.    a string, which means the most recent command beginning with that
  38.    string.
  39.  
  40.    -e ENAME selects which editor to use.  Default is FCEDIT, then EDITOR,
  41.       then the editor which corresponds to the current readline editing
  42.       mode, then vi.
  43.  
  44.    -l means list lines instead of editing.
  45.    -n means no line numbers listed.
  46.    -r means reverse the order of the lines (making it newest listed first).
  47.  
  48.      fc -e - [pat=rep ...] [command]
  49.      fc -s [pat=rep ...] [command]
  50.  
  51.    Equivalent to !command:sg/pat/rep execpt there can be multiple PAT=REP's.
  52. */
  53.  
  54. static char *fc_dosubs (), *fc_replace (), *fc_gethist (), *fc_readline ();
  55. static int fc_gethnum ();
  56. static void fc_replhist (), fc_addhist ();
  57.  
  58. /* Data structure describing a list of global replacements to perform. */
  59. typedef struct repl {
  60.   struct repl *next;
  61.   char *pat;
  62.   char *rep;
  63. } REPL;
  64.  
  65. #define USAGE    "usage: fc [-e ename] [-nlr] [first] [last] or fc -s [pat=rep] [command]"
  66.  
  67. /* True if A is the first substring in B. */
  68. #define prefix(a, b) (strncmp ((a), (b), strlen ((a))) == 0)
  69.  
  70. /* Accessors for HIST_ENTRY lists that are called HLIST. */
  71. #define histline(i) (hlist[(i)]->line)
  72. #define histdata(i) (hlist[(i)]->data)
  73.  
  74. #define FREE_RLIST() \
  75.     do { \
  76.         for (rl = rlist; rl; ) { \
  77.             REPL *r;    \
  78. \
  79.             r = rl->next; \
  80.             if (rl->pat) \
  81.                 free (rl->pat); \
  82.             if (rl->rep) \
  83.                 free (rl->rep); \
  84.             free (rl); \
  85.             rl = r; \
  86.         } \
  87.     } while (0)
  88.  
  89. /* String to execute on a file that we want to edit. */
  90. #define FC_EDIT_COMMAND "${FCEDIT:-${EDITOR:-vi}}"
  91.  
  92. int
  93. fc_builtin (list)
  94.      WORD_LIST *list;
  95. {
  96.   register int i;
  97.   register char *sep;
  98.   int numbering, reverse, listing, execute;
  99.   int histbeg, histend, last_hist, retval, first;
  100.   FILE *stream;
  101.   REPL *rlist = (REPL *) NULL, *rl;
  102.   char *ename = NULL, *command, *newcom, *line;
  103.   HIST_ENTRY **hlist;
  104.   char fn[MAXPATHLEN];
  105.  
  106.   numbering = 1;
  107.   reverse = listing = execute = 0;
  108.  
  109.   /* Parse out the options and set which of the two forms we're in. */
  110.   while (list && *list->word->word == '-')
  111.     {
  112.       register char *s = &((list->word->word)[1]);
  113.       register int c;
  114.  
  115.       if (!isletter (*s))    /* for stuff like fc -e - -2 */
  116.     break;
  117.  
  118.       while (c = *s++)
  119.     {
  120.       switch (c)
  121.         {
  122.         case 'n':
  123.           numbering = 0;
  124.           break;
  125.  
  126.         case 'l':
  127.           listing = 1;
  128.           break;
  129.  
  130.         case 'r':
  131.           reverse = 1;
  132.           break;
  133.  
  134.         case 's':
  135.           execute = 1;
  136.           break;
  137.  
  138.         case 'e':
  139.           list = list->next;
  140.  
  141.           if (list == NULL)
  142.         {
  143.           builtin_error (USAGE);
  144.           return (EXECUTION_FAILURE);
  145.         }
  146.  
  147.           ename = list->word->word;
  148.           break;
  149.  
  150.         default:
  151.           builtin_error (USAGE);
  152.           return (EXECUTION_FAILURE);
  153.         }
  154.     }
  155.       list = list->next;
  156.     }
  157.  
  158.   if (ename && (*ename == '-') && (ename[1] == '\0'))
  159.     execute = 1;
  160.  
  161.   /* The "execute" form of the command (re-run, with possible string
  162.      substitutions). */
  163.   if (execute)
  164.     {
  165.       while (list && ((sep = (char *)index (list->word->word, '=')) != NULL))
  166.     {
  167.       *sep++ = '\0';
  168.       rl = (REPL *)xmalloc (sizeof (REPL));
  169.       rl->next = (REPL *)NULL;
  170.       rl->pat = savestring (list->word->word);
  171.       rl->rep = savestring (sep);
  172.  
  173.       if (rlist == NULL)
  174.         rlist = rl;
  175.       else
  176.         {
  177.           rl->next = rlist;
  178.           rlist = rl;
  179.         }
  180.       list = list->next;
  181.     }
  182.  
  183.       /* If we have a list of substitutions to do, then reverse it
  184.      to get the replacements in the proper order. */
  185.  
  186.       if (rlist && rlist->next)
  187.     rlist = (REPL *) reverse_list ((GENERIC_LIST *) rlist);
  188.  
  189.       hlist = history_list ();
  190.  
  191.       /* If we still have something in list, it is a command spec.
  192.      Otherwise, we use the most recent command in time. */
  193.       if (list)
  194.     command = fc_gethist (list->word->word, hlist);
  195.       else
  196.     command = fc_gethist ((char *) NULL, hlist);
  197.  
  198.       if (command == NULL)
  199.     {
  200.       builtin_error ("no command found");
  201.       if (rlist)
  202.         FREE_RLIST ();
  203.  
  204.       return (EXECUTION_FAILURE);
  205.     }
  206.  
  207.       if (rlist)
  208.     {
  209.       newcom = fc_dosubs (command, rlist);
  210.       free (command);
  211.       FREE_RLIST ();
  212.       command = newcom;
  213.     }
  214.  
  215.       printf ("%s\n", command);
  216.       fc_replhist (command);    /* replace `fc -e -' with command */
  217.       return (parse_and_execute (command, "fc"));
  218.     }
  219.  
  220.   /* This is the second form of the command (the list-or-edit-and-rerun
  221.      form). */
  222.   hlist = history_list ();
  223.   for (i = 0; hlist[i]; i++);
  224.  
  225.   /* With the Bash implementation of history, the current command line
  226.      ("fc blah..." and so on) is already part of the history list by
  227.      the time we get to this point.  This just skips over that command
  228.      and makes the last command that this deals with be the last command
  229.      the user entered before the fc. */
  230.  
  231.   last_hist = i - 2;
  232.  
  233.   if (list)
  234.     {
  235.       histbeg = fc_gethnum (list->word->word, hlist);
  236.       list = list->next;
  237.  
  238.       if (list)
  239.     histend = fc_gethnum (list->word->word, hlist);
  240.       else
  241.     {
  242.       if (listing)
  243.         histend = last_hist;
  244.       else
  245.         histend = histbeg;
  246.     }
  247.     }
  248.   else
  249.     {
  250.       /* The default for listing is the last 16 history items. */
  251.       if (listing)
  252.     {
  253.       histend = last_hist;
  254.       histbeg = histend - 16;
  255.     }
  256.       else
  257.     {
  258.       /* For editing, it is the last history command. */
  259.       histbeg = histend = last_hist;
  260.       }
  261.     }
  262.  
  263.   /* We print error messages for line specifications out of range. */
  264.   if ((histbeg < 0) || (histend < 0) ||
  265.       (histbeg > last_hist) || (histend > last_hist))
  266.     {
  267.       builtin_error ("history specification out of range");
  268.       return (EXECUTION_FAILURE);
  269.     }
  270.  
  271.   if (histend < histbeg)
  272.     {
  273.       int t = histend;
  274.  
  275.       histend = histbeg;
  276.       histbeg = t;
  277.       reverse = 1;
  278.     }
  279.  
  280.   if (listing)
  281.     stream = stdout;
  282.   else
  283.     {
  284.       numbering = 0;
  285.       sprintf (fn, "/tmp/bash%d", (int)time ((long *) 0) + (int)getpid ());
  286.  
  287.       stream = fopen (fn, "w");
  288.  
  289.       if (!stream)
  290.     {
  291.       builtin_error ("cannot open temp file %s", fn);
  292.       return (EXECUTION_FAILURE);
  293.     }
  294.     }
  295.  
  296.   if (!reverse)
  297.     {
  298.       for (i = histbeg; i <= histend; i++)
  299.     {
  300.       QUIT;
  301.       if (numbering)
  302.         fprintf (stream, "%d\t%c", i + history_base,
  303.              histdata (i) ? '*' : ' ');
  304.  
  305.       fprintf (stream, "%s\n", histline (i));
  306.     }
  307.     }
  308.   else
  309.     {
  310.       for (i = histend; i >= histbeg; i--)
  311.     {
  312.       QUIT;
  313.       if (numbering)
  314.         fprintf (stream, "%d\t%c", i + history_base,
  315.              histdata (i) ? '*' : ' ');
  316.       fprintf (stream, "%s\n", histline (i));
  317.     }
  318.     }
  319.  
  320.   if (listing)
  321.     return (EXECUTION_SUCCESS);
  322.  
  323.   fclose (stream);
  324.  
  325.   /* Now edit the file of commands. */
  326.   if (ename)
  327.     {
  328.       command = (char *)xmalloc (strlen (ename) + strlen (fn) + 2);
  329.       sprintf (command, "%s %s", ename, fn);
  330.     }
  331.   else
  332.     {
  333.       command = (char *)xmalloc (3 + strlen (FC_EDIT_COMMAND) + strlen (fn));
  334.       sprintf (command, "%s %s", FC_EDIT_COMMAND, fn);
  335.     }
  336.   parse_and_execute (command, "fc");
  337.  
  338.   /* Now reopen the file and execute the edited commands. */
  339.  
  340.   stream = fopen (fn, "r");
  341.  
  342.   if (stream == NULL)
  343.     {
  344.       builtin_error ("cannot reopen temp file %s", fn);
  345.       unlink (fn);
  346.       return (EXECUTION_FAILURE);
  347.     }
  348.  
  349.   retval = EXECUTION_SUCCESS;
  350.   first = 1;
  351.  
  352.   /* First, write the commands to the history file.  This will not happen
  353.      when we call parse_and_execute, since parse_and_execute disables
  354.      the command line history while it executes. */
  355.      
  356.   while ((line = fc_readline (stream)) != NULL)
  357.     {
  358.       if (line[0] == '\n')
  359.     {
  360.       free (line);
  361.       continue;        /* Skip blank lines. */
  362.     }
  363.  
  364.       if (first)
  365.     {
  366.       first = 0;
  367.       fc_replhist (line);
  368.     }
  369.       else
  370.     fc_addhist (line);
  371.     }
  372.   fclose (stream);
  373.  
  374.   {
  375.     extern int echo_input_at_read;
  376.     extern int unlink ();
  377.  
  378.     /* Turn on the `v' flag while maybe_execute_file runs so the commands
  379.        will be echoed as they are read by the parser. */
  380.     begin_unwind_frame ("fc builtin");
  381.     add_unwind_protect (unlink, fn);
  382.     unwind_protect_int (echo_input_at_read);
  383.     echo_input_at_read = 1;
  384.     
  385.     retval = maybe_execute_file (fn);
  386.  
  387.     run_unwind_frame ("fc builtin");
  388.   }
  389.  
  390.   return (retval);
  391. }
  392.  
  393. /* Return an absolute index into HLIST which corresponds to COMMAND.  If
  394.    COMMAND is a number, then it was specified in relative terms.  If it
  395.    is a string, then it is the start of a command line present in HLIST. */
  396. static int
  397. fc_gethnum (command, hlist)
  398.      char *command;
  399.      HIST_ENTRY **hlist;
  400. {
  401.   int sign = 1, n;
  402.   register int i, j;
  403.   register char *s;
  404.  
  405.   /* Count history elements. */
  406.   for (i = 0; hlist[i]; i++);
  407.  
  408.   /* With the Bash implementation of history, the current command line
  409.      ("fc blah..." and so on) is already part of the history list by
  410.      the time we get to this point.  This just skips over that command
  411.      and makes the last command that this deals with be the last command
  412.      the user entered before the fc. */
  413.   i -= 2;
  414.  
  415.   /* No specification defaults to most recent command. */
  416.   if (command == NULL)
  417.     return (i);
  418.  
  419.   /* Otherwise, there is a specification.  It can be a number relative to
  420.      the current position, or an absolute history number. */
  421.   s = command;
  422.  
  423.   /* Handle possible leading minus sign. */
  424.   if (s && (*s == '-'))
  425.     {
  426.       sign = -1;
  427.       s++;
  428.     }
  429.  
  430.   if (s && digit(*s))
  431.     {
  432.       n = atoi (s);
  433.       n *= sign;
  434.  
  435.       /* Anything specified greater than the last history element that we
  436.      deal with is an error. */
  437.       if (n > i + history_base)
  438.     return (-1);
  439.  
  440.       /* If the value is negative or zero, then it is an offset from
  441.      the current history item. */
  442.       if (n <= 0)
  443.     return (i + n);
  444.  
  445.       return (n - history_base);
  446.     }
  447.  
  448.   for (j = i; j >= 0; j--)
  449.     {
  450.       if (prefix (command, histline (j)))
  451.     return (j);
  452.     }
  453.   return (-1);
  454. }
  455.  
  456. /* Locate the most recent history line which begins with
  457.    COMMAND in HLIST, and return a malloc()'ed copy of it. */
  458. static char *
  459. fc_gethist (command, hlist)
  460.      char *command;
  461.      HIST_ENTRY **hlist;
  462. {
  463.   int i;
  464.  
  465.   if (!hlist)
  466.     return ((char *)NULL);
  467.  
  468.   i = fc_gethnum (command, hlist);
  469.  
  470.   if (i >= 0)
  471.     return (savestring (histline (i)));
  472.   else
  473.     return ((char *)NULL);
  474. }
  475.  
  476. /* Read the edited history lines from STREAM and return them
  477.    one at a time.  This can read unlimited length lines.  The
  478.    caller should free the storage. */
  479. static char *
  480. fc_readline (stream)
  481.      FILE *stream;
  482. {
  483.   register int c;
  484.   int line_len = 0, lindex = 0;
  485.   char *line = (char *)NULL;
  486.  
  487.   while ((c = getc (stream)) != EOF)
  488.     {
  489.       if ((lindex + 2) >= line_len)
  490.     line = (char *) xrealloc (line, (line_len += 128));
  491.  
  492.       if (c == '\n')
  493.     {
  494.       line[lindex++] = '\n';
  495.       line[lindex++] = '\0';
  496.       return (line);
  497.     }
  498.       else
  499.     line[lindex++] = c;
  500.     }
  501.  
  502.   if (!lindex)
  503.     {
  504.       if (line)
  505.     free (line);
  506.  
  507.       return ((char *)NULL);
  508.     }
  509.  
  510.   if (lindex + 2 >= line_len)
  511.     line = (char *)xrealloc (line, lindex + 3);
  512.  
  513.   line[lindex++] = '\n';        /* Finish with newline if none in file */
  514.   line[lindex++] = '\0';
  515.   return (line);
  516. }
  517.  
  518. /* Perform the SUBS on COMMAND.
  519.    SUBS is a list of substitutions, and COMMAND is a simple string.
  520.    Return a pointer to a malloc'ed string which contains the substituted
  521.    command. */
  522. static char *
  523. fc_dosubs (command, subs)
  524.      char *command;
  525.      REPL *subs;
  526. {
  527.   register char *new = savestring (command);
  528.   register REPL *r;
  529.  
  530.   for (r = subs; r; r = r->next)
  531.     {
  532.       register char *t;
  533.  
  534.       t = fc_replace (r->pat, r->rep, new);
  535.       free (new);
  536.       new = t;
  537.     }
  538.   return (new);
  539. }
  540.  
  541. /* Replace the occurrences of PAT with REP in COMMAND.
  542.    This returns a new string; the caller should free it. */
  543. static char *
  544. fc_replace (pat, rep, command)
  545.      char *pat, *rep, *command;
  546. {
  547.   register int i;
  548.   int patlen, replen, templen;
  549.   char *new, *temp;
  550.  
  551.   patlen = strlen (pat);
  552.   replen = strlen (rep);
  553.  
  554.   temp = savestring (command);
  555.   templen = strlen (temp);
  556.   i = 0;
  557.  
  558.   for (; (i + patlen) <= templen; i++)
  559.     {
  560.       if (strncmp (temp + i, pat, patlen) == 0)
  561.     {
  562.       new = (char *) xmalloc (1 + (replen - patlen) + templen);
  563.  
  564.       strncpy (new, temp, i);
  565.       strncpy (new + i, rep, replen);
  566.       strncpy (new + i + replen,
  567.            temp + i + patlen, templen - (i + patlen));
  568.       new[templen + (replen - patlen)] = '\0'; /* just in case */
  569.  
  570.       free (temp);
  571.       temp = new;
  572.       i += replen;
  573.       templen = strlen (temp);
  574.     }
  575.     }
  576.   return (temp);
  577. }
  578.  
  579. /* Use `command' to replace the last entry in the history list, which,
  580.    by this time, is `fc blah...'.  The intent is that the new command
  581.    become the history entry, and that `fc' should never appear in the
  582.    history list.  This way you can do `r' to your heart's content.
  583.  
  584.    Should this do anything with the history_control variable? */
  585. static void
  586. fc_replhist (command)
  587.      char *command;
  588. {
  589.   register int i;
  590.   HIST_ENTRY **hlist, *histent, *discard, *history_get ();
  591.   char *data;
  592.   int n;
  593.  
  594.   if (command == NULL || *command == NULL)
  595.     return;
  596.  
  597.   hlist = history_list ();
  598.  
  599.   if (hlist == NULL)
  600.     return;
  601.  
  602.   for (i = 0; hlist[i]; i++);
  603.   i--;
  604.  
  605.   /* History_get () takes a parameter that should be
  606.      offset by history_base. */
  607.  
  608.   histent = history_get (history_base + i);    /* Don't free this */
  609.   if (histent == NULL)
  610.     return;
  611.  
  612.   if (histent->data)
  613.     data = savestring (histent->data);
  614.   else
  615.     data = (char *) NULL;
  616.  
  617.   n = strlen (command);
  618.  
  619.   if (command[n - 1] == '\n')
  620.     command[n - 1] = '\0';
  621.  
  622.   if (command && *command)
  623.     {
  624.       discard = replace_history_entry (i, command, data);
  625.       if (discard)
  626.     {
  627.       if (discard->line)
  628.         free (discard->line);
  629.  
  630.       free ((char *) discard);
  631.     }
  632.     }
  633. }
  634.  
  635. /* Add LINE to the history, after removing a single trailing newline. */
  636. static void
  637. fc_addhist (line)
  638.      char *line;
  639. {
  640.   register int n = strlen (line);
  641.  
  642.   if (line[n - 1] == '\n')
  643.     line[n - 1] = '\0';
  644.  
  645.   if (line && *line)
  646.     add_history (line);
  647. }
  648.